﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.ERPFramework.BLLInterface.Session;
using Kugar.Core;
using Kugar.ERPFramework.Core;
using System.ServiceModel;
using System.Runtime.Caching;

namespace Kugar.ERPFramework.Service
{
    public class CustomSession : ICustomSession
    {
        private MemoryCache _cache=new MemoryCache("Session");
        private static CustomSession _default=new CustomSession();
        
        public SessionItem this[string sessionID]
        {
            get
            {
                var sessionItem = (SessionItem)_cache.Get(sessionID);

                if (sessionItem==null)
                {
                    sessionItem = new SessionItem(sessionID);

                    var s = new CacheItemPolicy();
                    s.SlidingExpiration=new TimeSpan(0,10,0);

                    _cache.Add(sessionID, sessionItem, s);
                }

                return sessionItem;
            }
        }

        public SessionItem CurrentSession
        {
            get { return this[OperationContext.Current.SessionId]; }
        }
        public void Remvoe(string sessionID)
        {
            _cache.Remove(sessionID);
        }

        public static ICustomSession Default
        {
            get { return _default; }
        }
    }
}
